package evi.com.evi.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.List;
import java.util.ArrayList;

import evi.com.evi.R;
import evi.com.evi.bean.DisposeOrderBean;
import evi.com.evi.bean.SubOrderDetailBean;
import evi.com.evi.pager.ToPickUpOrderPager;
import evi.com.evi.utils.AppUtils;
import evi.com.evi.utils.MessageDialog;
import evi.com.evi.utils.OrderStatus;
import evi.com.evi.utils.ToastUtils;
import evi.com.evi.view.MyGridView;

/**
 * Created by YX_BOSS on 2017/3/11.
 */

public class ToPickUpOrderAdapter extends RecyclerView.Adapter<ToPickUpOrderAdapter.MyViewHolder> implements Filterable {
    private static final String TAG = "TAG";
    private Context context;
    private List<DisposeOrderBean> quHuoList;
    //用来存储过滤的结果集
    private List<DisposeOrderBean> copyQuHuoList;
    private ToPickUpOrderPager toPickUpOrderPager;

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_order_time;        //时间
        TextView tv_order;
        TextView tv_text_3;
        TextView tv_user;                //收货人

        TextView tv_phone;                //收货人号码
        TextView tv_money;                //订单金额
        TextView tv_btn_manager;       //按钮
        FrameLayout fl_goods;            //商品表

        MyGridView gv_goods;      //商品表

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_order = (TextView) itemView.findViewById(R.id.tv_order);
            tv_user = (TextView) itemView.findViewById(R.id.tv_user);//
            tv_phone = (TextView) itemView.findViewById(R.id.tv_phone);//
            tv_text_3 = (TextView) itemView.findViewById(R.id.tv_text_3);//出发时间

            tv_order_time = (TextView) itemView.findViewById(R.id.tv_order_time);


            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_btn_manager = (TextView) itemView.findViewById(R.id.tv_btn_manager);

            fl_goods = (FrameLayout) itemView.findViewById(R.id.fl_goods);
            gv_goods = (MyGridView) itemView.findViewById(R.id.gv_goods);
        }
    }

    public ToPickUpOrderAdapter(Context context, List<DisposeOrderBean> quHuoList, ToPickUpOrderPager toPickUpOrderPager) {
        this.context = context;
        this.quHuoList = quHuoList;
        this.copyQuHuoList = new ArrayList<DisposeOrderBean>();
        this.copyQuHuoList.addAll(quHuoList);
        this.toPickUpOrderPager = toPickUpOrderPager;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_lv_accept_order_dispose, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final DisposeOrderBean bean = quHuoList.get(position);
        Log.e(TAG, "ToPickUpOrderAdapter.onBindViewHolder:" + bean);
        holder.tv_order_time.setText(bean.getOrder_time());
        holder.tv_order.setText(bean.getOrder());

        holder.tv_user.setText(bean.getName());
        holder.tv_phone.setText(bean.getTelnum());
        holder.tv_text_3.setText(bean.getTime());
        holder.tv_money.setText(bean.getMoney());
        holder.tv_btn_manager.setText("已到店取货");

        List<SubOrderDetailBean> foodList = bean.getFoodList();
        GridGoodsAdapter goodsAdapter = new GridGoodsAdapter(context, foodList);
        holder.gv_goods.setAdapter(goodsAdapter);


        //拨打电话
        holder.tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone(bean.getTelnum());
            }
        });

        holder.tv_btn_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtils.showMsgDialog(context, "已到店取货", "确认", "取消", 1, null, new AppUtils.MsgCallBack() {
                    @Override
                    public void confirmMsg(MessageDialog d) {
                        if (bean.getDeliverType() != null && bean.getDeliverType() == OrderStatus.DELIVER_TYPE_SEND) {
                            toPickUpOrderPager.sendDealOrderRequest(bean.getMainOrderId(), OrderStatus.MAINORDER_STATUS_SERVICER_GET_GOOD, "", position);
                        } else {
                            toPickUpOrderPager.sendDealOrderRequest(bean.getMainOrderId(), OrderStatus.MAINORDER_STATUS_COMPLETED, "", position);
                        }
                        d.dismiss();
                    }

                    @Override
                    public void cancelMsg(MessageDialog d) {
                        ToastUtils.showMsg(context, "取消");
                        d.dismiss();
                    }
                });
            }
        });

    }

    /**
     * 拨打电话
     *
     * @param phone 电话号
     */

    public void phone(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return quHuoList.size();
    }


    public void clearResult() {
        if (quHuoList != null) quHuoList.clear();
        if (copyQuHuoList != null) copyQuHuoList.clear();
    }

    public void addResult(DisposeOrderBean bean) {
        if (quHuoList == null) {
            quHuoList = new ArrayList<DisposeOrderBean>();
        }
        if (copyQuHuoList == null) {
            copyQuHuoList = new ArrayList<DisposeOrderBean>();
        }
        quHuoList.add(bean);
        copyQuHuoList.add(bean);
    }

    public int getResultSize() {
        return (quHuoList != null) ? quHuoList.size() : 0;
    }

    @Override
    public Filter getFilter() {
        return new ToPickUpOrderFilter(this);
    }

    /**
     * 过滤类，根据输入的电话或者订单号过滤
     */
    private static class ToPickUpOrderFilter extends Filter {

        private final ToPickUpOrderAdapter adapter;

        private ToPickUpOrderFilter(ToPickUpOrderAdapter adapter) {
            super();
            this.adapter = adapter;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            //初始化过滤结果对象
            final FilterResults results = new FilterResults();
            //假如搜索为空的时候，将复制的数据添加到原始数据，用于继续过滤操作
            if (results.values == null) {
                adapter.quHuoList.clear();
                adapter.quHuoList.addAll(adapter.copyQuHuoList);
            }
            //关键字为空的时候，搜索结果为复制的结果
            if (constraint.length() == 0) {
                results.values = adapter.copyQuHuoList;
                results.count = adapter.copyQuHuoList.size();
            } else {
                final ArrayList<DisposeOrderBean> newValues = new ArrayList<DisposeOrderBean>();
                for (final DisposeOrderBean user : this.adapter.copyQuHuoList) {
                    if (user.getTelnum() != null && user.getTelnum().contains(constraint)) {
                        newValues.add(user);
                    } else if (user.getOrder() != null && user.getOrder().contains(constraint)) {
                        newValues.add(user);
                    }
                }
                results.values = newValues;
                results.count = newValues.size();
//                Log.e(TAG,"===new Value="+newValues.size());

            }
            Log.e(TAG, "--执行performFilter.constraint=" + constraint + ",copyJiedanList.size=" + this.adapter.copyQuHuoList.size() + ",result=" + (((ArrayList<DisposeOrderBean>) results.values).size()));
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.e(TAG, "--ToPickUpOrderAdapter.publishResults.constraint=" + constraint + ",originalList.size=" + this.adapter.copyQuHuoList.size() + ",result=" + (((ArrayList<DisposeOrderBean>) results.values).size()));
            adapter.quHuoList.clear();//清除原始数据
            adapter.quHuoList.addAll((ArrayList<DisposeOrderBean>) results.values);//将过滤结果添加到这个对象

            adapter.toPickUpOrderPager.disposeFalseFragment.getCount(adapter.toPickUpOrderPager, "" + results.count);

            if (results.count > 0) {
                adapter.notifyDataSetChanged();//有关键字的时候刷新数据
            } else {
                //关键字不为零但是过滤结果为空刷新数据
                if (constraint.length() != 0) {
                    adapter.notifyDataSetChanged();
                    return;
                }
                //加载复制的数据，即为最初的数据
//                adapter.quHuoList.addAll(adapter.copyQuHuoList);
            }
        }
    }
}
